
package org.thingsboard.server.remote.upload.gateway;

import org.thingsboard.server.common.data.kv.KvEntry;
import org.thingsboard.server.common.data.kv.TsKvEntry;
import org.thingsboard.server.remote.upload.data.*;


import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;

/**
 * 上传数据到远程IOT平台
 *  核心接口
 */
public interface GatewayService {

    /**
     * 初始化
     * @throws Exception
     */
    void init() throws Exception;


    /**
     * 销毁操作
     * @throws Exception
     */
    void destroy() throws Exception;

    String getTenantLabel();
    boolean isUsable();

    /**
     *   下属设备连接 报告
     *
     * @param deviceName 设备名称
     * @param deviceType 设备类型
     */
    MqttDeliveryFuture onDeviceConnect(String deviceName, String deviceType);

    /**
     * 下属设备掉线报告 设备名称
     *
     * @param deviceName
     */
    Optional<MqttDeliveryFuture> onDeviceDisconnect(String deviceName);

    /**
     * 上报设备属性更新
     *        设备名称
     * @param deviceName - the device name
     * @param attributes - the attribute values list
     */
    MqttDeliveryFuture onDeviceAttributesUpdate(String deviceName, List<KvEntry> attributes);

    /**
     * 上报设备遥测
     *
     * @param deviceName - the device name
     * @param telemetry  - the telemetry values list
     */
    MqttDeliveryFuture onDeviceTelemetry(String deviceName, List<TsKvEntry> telemetry);

    /**
     * 上报属性请求
     *
     * @param attributeRequest - attributes request
     * @param listener         - attributes response
     */
    void onDeviceAttributeRequest(AttributeRequest attributeRequest, Consumer<AttributeResponse> listener);

    /**
     * 上报 TB RPC调用结果
     *
     * @param response - the device response to RPC call
     */
    void onDeviceRpcResponse(RpcCommandResponse response);

    /**
     * 订阅共享属性的更新
     *
     * @param subscription - the subscription
     * @return true if successful, false if already subscribed
     */
    boolean subscribe(AttributesUpdateSubscription subscription);

    /**
     * 订阅请求
     *
     * @param subscription - the subscription
     * @return true if successful, false if already subscribed
     */
    boolean subscribe(RpcCommandSubscription subscription);

    /**
     * 取消订阅主题
     * Unsubscribe to attribute updates from Thingsboard
     *
     * @param subscription - the subscription
     * @return true if successful, false if already unsubscribed
     */
    boolean unsubscribe(AttributesUpdateSubscription subscription);

    /**
     * Unsubscribe to server-side rpc commands from Thingsboard
     * 取消订阅主题
     * @param subscription - the subscription
     * @return true if successful, false if already unsubscribed
     */
    boolean unsubscribe(RpcCommandSubscription subscription);

    /**
     * Report generic error from one of gateway components
     *
     * @param e - the error
     */
    void onError(Exception e);

    /**
     * Report error related to device
     *
     * @param deviceName - the device name
     * @param e          - the error
     */
    void onError(String deviceName, Exception e);

    /**
     * Report applied configuration
     *
     * @param configuration - extension configuration
     */
    void onAppliedConfiguration(String configuration);

}
